﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.NoticeManage
{
    public partial class NoticeReceiveList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindNoticeReceiveList(1);
            }
        }
        private void BindNoticeReceiveList(int pageindex)
        {
          
            int RecordCount = 0;
            List<CtrtMyNoticeEntity> list = new CtrtMyNoticeBll().GetAll<CtrtMyNoticeEntity>(" and NoticeID in (select NoticeID from Cms_Ctrt_MyNoticeUser where UserID ='" + base.CurrentUserID.ToString() + "')", "NoticeTime desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterNoticeReceives.DataSource = list;
            RepeaterNoticeReceives.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterNoticeReceives_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindNoticeReceiveList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            if (noticeids.Value.Trim() != "")
            {
                string[] noticeidsArray = noticeids.Value.Trim().Split(new char[] { ';' });
                for (int index = 0; index < noticeidsArray.Length; index++)
                {
                    List<CtrtMyNoticeUserEntity> entitys = new CtrtMyNoticeUserBll().GetAll<CtrtMyNoticeUserEntity>(" and NoticeID ='"+ noticeidsArray[index]+"'");
                    if (entitys != null && entitys.Count > 0)
                    {
                        CtrtMyNoticeUserEntity noticeUserEntity = entitys[0];
                        noticeUserEntity.Status = System.Convert.ToInt32(Enumerator.NoticeMessageStatus.read);
                        new CtrtMyNoticeUserBll().Update(noticeUserEntity);
                        BindNoticeReceiveList(1);
                    }
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindNoticeReceiveList(AspNetPager1.CurrentPageIndex);
        }

        protected void RepeaterNoticeReceives_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label status = e.Item.FindControl("status") as Label;
            Label NoticeID =e.Item.FindControl("NoticeID") as Label;
            Label CreatedBy = e.Item.FindControl("CreatedBy") as Label;
            Label CreatedUserName = e.Item.FindControl("CreatedUserName") as Label;
            if (status != null) {
                List<CtrtMyNoticeUserEntity> entitys = new CtrtMyNoticeUserBll().GetAll<CtrtMyNoticeUserEntity>(" and NoticeID = '" +NoticeID.Text + "'");
                if (entitys[0].Status == System.Convert.ToInt32(Enumerator.NoticeMessageStatus.read))
                {
                    status.Text = "已读";
                } else {
                    status.Text = "未读";
                }

                SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(CreatedBy.Text));
                CreatedUserName.Text = entity.UserName;
            }
        } 
    }
}
